{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 【Task 2】\n",
    "特征衍生  \n",
    "特征挑选  分别用IV值和随机森林等进行特征选择  \n",
    "……以及你能想到特征工程处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### iv值特征选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def CalcIV(Xvar,Yvar):\n",
    "    N_0=np.sum(Yvar==0)\n",
    "    N_1=np.sum(Yvar==1)\n",
    "    N_0_group=np.zeros(np.unique(Xvar).shape)\n",
    "    \n",
    "    N_1_group=np.zeros(np.unique(Xvar).shape)\n",
    "    for i in range(len(np.unique(Xvar))):\n",
    "        N_0_group[i] = Yvar[(Xvar==np.unique(Xvar)[i])&(Yvar==0)].count()\n",
    "        N_1_group[i] = Yvar[(Xvar==np.unique(Xvar)[i])&(Yvar==1)].count()\n",
    "    iv = np.sum((N_0_group/N_0-N_1_group/N_1)*np.log((N_0_group/N_0)/(N_1_group/N_1)))\n",
    "    if iv>=1.0:## 处理极端值\n",
    "        iv=1\n",
    "    return iv\n",
    "\n",
    "def caliv_batch(df,Yvar):\n",
    "    ivlist=[]\n",
    "    for col in df.columns:\n",
    "        iv=CalcIV(df[col],Yvar)\n",
    "        ivlist.append(iv)\n",
    "    names=list(df.columns)\n",
    "    iv_df=pd.DataFrame({'Var':names,'Iv':ivlist},columns=['Var','Iv'])\n",
    "\n",
    "    return iv_df,ivlist\n",
    "im_iv, ivl = caliv_batch(data_prepared.iloc[:,:-1],data_prepared.iloc[:,-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 随机森林选择特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "x_train = data_prepared.iloc[:,:-1]\n",
    "y_train = data_prepared.iloc[:,-1]\n",
    "feat_lables = x_train.columns\n",
    "forest = RandomForestClassifier(n_estimators=10000, random_state=0,n_jobs=1)\n",
    "forest.fit(x_train, y_train)\n",
    "importance = forest.feature_importances_\n",
    "imp_result = np.argsort(importance)[::-1]\n",
    "\n",
    "for i in range(x_train.shape[1]):\n",
    "    print(\"%2d. %-*s %f\"%(i+1, 30, feat_lables[i], importance[imp_result[i]]))"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:tensorflow-gpu]",
   "language": "python",
   "name": "conda-env-tensorflow-gpu-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
